<template>
    <view>
        <!--        <news v-if="PageCur==='news'"></news>-->
        <!--        <me v-if="PageCur==='me'"></me>-->
        <dev v-bind:devices="devices" v-if="PageCur==='devices'" v-bind:statistics="statistics"></dev>
        <moneybag v-bind:statistics="statistics" v-if="PageCur==='moneybag'"></moneybag>
        <about v-if="PageCur==='about'"></about>
        <view class="cu-tabbar-height"></view>
        <view class="cu-bar tabbar shadow bg-black foot">
            <view :class="PageCur==='devices'?'text-yellow':'text-gray'" @click="NavChange" class="action "
                  data-cur="devices">
                <view class="cuIcon-mobilefill"></view>
                设备
            </view>
            <!-- <view class="action text-gray">
                 <view class="cuIcon-similar"></view>
                 分类
             </view>
             <view class="action text-gray">
                 <view class="cuIcon-recharge"></view>
                 积分
             </view>
             <view class="action text-gray">
                 <view class="cuIcon-cart">
                     <view class="cu-tag badge">99</view>
                 </view>
                 购物车
             </view> -->
            <view :class="PageCur==='moneybag'?'text-yellow':'text-gray'" @click="NavChange" class="action "
                  data-cur="moneybag">
                <view class="cuIcon-redpacket_fill">

                </view>
                钱包
            </view>
            <view :class="PageCur==='about'?'text-yellow':'text-gray'" @click="NavChange" class="action "
                  data-cur="about">
                <view class="cuIcon-infofill">

                </view>
                关于
            </view>
        </view>
    </view>
</template>

<script lang="ts">
    import Vue from 'vue';
    import dev from "@/pages/index/devices.vue";
    import about from "@/pages/about/about.vue";
    import moneybag from "@/pages/moneybag/moneybag.vue";
    import cash from "@/pages/my-cash/my-cash.vue";
    import {appInfo} from "@/common/helper";
    import {http} from "@/utils/utils";

    export default Vue.extend({
        components: {
            dev, moneybag, about
        },
        data() {
            return {
                PageCur: 'devices',
                news: [],
                devices: [],
                statistics: {
                    cashOut: 0.00,
                    dailyBonus: 0.00,
                    deviceCount: 0,
                    onlineCount: 0,
                    totalAmount: 0.00,
                    surplus: 0
                }
            };
        },
        onLoad() {
            uni.$on("Authorization", () => {
                uni.showLoading();
                http.get("api/ma/my-devices").then((res: any) => {
                    uni.hideLoading();
                    console.log(res);
                    this.devices = res.data
                }).finally(() => uni.hideLoading());

                uni.showLoading({title: '加载中', mask: true});
                http.get("api/ma/statistics", {unionId: appInfo.session.unionid}).then((res: any) => {
                    this.statistics = res.data;
                    console.log(this.statistics);
                    this.statistics.surplus = this.statistics.totalAmount - this.statistics.cashOut
                }).finally(() => {
                    uni.hideLoading();
                })
            });
        },
        methods: {
            NavChange(e: any) {
                this.PageCur = e.currentTarget.dataset.cur;
                // if (this.PageCur === "cash") {
                //     uni.showLoading({title: '加载中', mask: true});
                //     http.get("api/ma/statistics", {unionId: appInfo.session.unionid}).then((res: any) => {
                //         this.statistics = res.data;
                //         this.statistics.surplus = this.statistics.totalAmount - this.statistics.cashOut
                //     }).finally(() => {
                //         uni.hideLoading();
                //     })
                // }
            },
            reload() {
                http.get("api/ma/statistics", {unionId: appInfo.session.unionid}).then((res: any) => {
                    const data = res.data;
                    console.log(data);
                    appInfo.statistics = res.data;
                    uni.$emit('statistics', {})
                }).finally(() => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh()
                })
            }
        },
        onShow() {
            console.log("onShow index");
        },
        onPullDownRefresh() {
            this.reload()
        },
    });
</script>

<style>

</style>
